package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.SysRole;
import com.taobao.machinesystem.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/sys-role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色
     * @param currentPage
     * @param size
     * @return
     */
    @PostMapping("/list/{currentPage}/{size}")
    public Result list(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size") Integer size) {
        Page<SysRole> page = new Page<>(currentPage, size);
        sysRoleService.page(page);
        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total", page.getTotal())
                .setMapValue("items", page.getRecords());
    }

    @PostMapping("/create")
    public Result save(@RequestBody SysRole sysPermission) {
        if (Objects.isNull(sysPermission)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        boolean save = sysRoleService.save(sysPermission);

        if (save) {
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        } else {
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }

    /**
     * 更改信息
     * @param sysPermission
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody SysRole sysPermission) {
        if (!Objects.isNull(sysPermission)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = sysRoleService.updateById(sysPermission);

        if (flag) {
            log.info("update plan0 success {}", sysPermission.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", sysPermission.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除设备类型
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        boolean flag = sysRoleService.removeById(id);

        if (flag) {
            log.info("update plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("update plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }

    @PostMapping("/create/role/permission/{roleId}")
    public Result createRolePermission(@PathVariable String roleId,String[] permissionId){
        boolean createRolePermission =  sysRoleService.createRolePermission(roleId,permissionId);
        if (createRolePermission) {
            return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
        } else {
            return Result.error().setMessage(SystemConst.INSERT_FAIL);
        }
    }

    /**
     * 增加一个角色，新增的角色没有任何权限
     * @param role
     * @return
     */
    @PostMapping("/createRole")
    public Result createRole(@RequestBody SysRole role){
        this.sysRoleService.createRole(role);
        return Result.ok().setMessage(SystemConst.INSERT_SUCCESS);
    }
}

